package com.ui.basic.service.admin;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsecurity.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.basic.util.Globals;
import com.ui.basic.command.CommandMenu;
import com.ui.basic.dao.assembler.MenuAssembler;
import com.ui.basic.ibatis.dao.system.TblSMenuDAO;
import com.ui.basic.ibatis.model.system.TblSMenu;
import com.ui.basic.ibatis.model.system.TblSMenuExample;

@Service("menuService")
public class MenuServiceImpl implements MenuService {
private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory.getLog(MenuServiceImpl.class);

	@Autowired
	private TblSMenuDAO tblSMenuDAO;
	
	public void setTblSMenuDAO(TblSMenuDAO tblSMenuDAO) {
		this.tblSMenuDAO = tblSMenuDAO;
	}

	public CommandMenu getMenu(CommandMenu commandMenu) throws SQLException {
		LOGGER.debug(">>>MenuServiceImpl.getMenu");
		
		if (commandMenu != null) {
			TblSMenuExample example = new TblSMenuExample();

			int rec = 0;
			try {
				rec = tblSMenuDAO.countByExample(example);
			} catch (NullPointerException npe) {
				LOGGER.error(npe);
			}

			commandMenu.calculate(rec);
			example.setIntRowNum((commandMenu.getPgIndex() - 1)
					* commandMenu.getPgRows());
			example.setIntRowsPerPage(commandMenu.getPgRows());
			example.setOrderByClause("MENU_CODE");
			commandMenu.setData(tblSMenuDAO.selectByExample(example));
		}
		
		return commandMenu;
	}

	public CommandMenu getUserDetail(CommandMenu commandMenu)
			throws SQLException {
		LOGGER.debug(">>>MenuServiceImpl.getUserDetail");
		
		commandMenu.setProperty(tblSMenuDAO.selectByPrimaryKey(commandMenu.getId()));
		return commandMenu;
	}

	public void save(CommandMenu commandMenu) throws SQLException {
		LOGGER.debug(">>>MenuServiceImpl.save");
		MenuAssembler assembler = new MenuAssembler();
		TblSMenu menu = assembler.toEntity(commandMenu);

		if(commandMenu.getId() != null){
			menu.setChangedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			menu.setChangeddate(new Date());
			try {
				tblSMenuDAO.updateByPrimaryKeySelective(menu);

			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}else{
			menu.setCreatedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			menu.setCreateddate(new Date());
			try {
				tblSMenuDAO.insert(menu);
			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}
	}

	public void delete(CommandMenu commandMenu) throws SQLException {
		LOGGER.debug(">>>MenuServiceImpl.delete");
		TblSMenuExample example = new TblSMenuExample();
		
		if(commandMenu.getId()!=null){
			example.createCriteria().andIdEqualTo(commandMenu.getId());
		}else if(commandMenu.getSelectedRecord()!=null && commandMenu.getSelectedRecord().length>0){
			List<Long> idList = new ArrayList<Long>();
			for(String strId: commandMenu.getSelectedRecord())
				idList.add(Long.valueOf(strId));
			example.createCriteria().andIdIn(idList);
		}
		
		tblSMenuDAO.deleteByExample(example);	
	}

}
